package com.metronoteapp.dialog;

import java.io.IOException;
import android.app.Dialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.metronoteapp.R;
import com.metronoteapp.until.AudioRecorderUntil;

public class PlayingAudioDialog extends Dialog implements
		android.view.View.OnClickListener, OnSeekBarChangeListener, Runnable {
	private String TAG = PlayingAudioDialog.class.getSimpleName();
	private Context mContext;
	private TextView txtTitle;
	private Dialog dialog;
	private Button btnExit;
	private ImageView imgPlay;
	private AudioRecorderUntil mObjRecord;
	private String mPath;
	private Boolean mBlnPlay;
	private SeekBar mProgress;
	private MediaPlayer mPlayer = null;
	private Thread mThread;

	public PlayingAudioDialog(Context context) {
		super(context);
		mContext = context;
	}

	public void displaydialog(String _Path, String _title, String _date) {
		Log.v(TAG, "Path : " + _Path);
		Log.v(TAG, "Title : " + _title);
		mBlnPlay = true;
		mPath = _Path;
		dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		WindowManager.LayoutParams lp = this.getWindow().getAttributes();
		lp.dimAmount = 0;
		dialog.setContentView(R.layout.layout_view_note_audio);
		dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

		txtTitle = (TextView) dialog.findViewById(R.id.txtTitlePlayAudio);
		imgPlay = (ImageView) dialog.findViewById(R.id.imgPlayNoteAudio);
		btnExit = (Button) dialog.findViewById(R.id.btnExitPlaying);
		mProgress = (SeekBar) dialog.findViewById(R.id.seekbar);

		mObjRecord = new AudioRecorderUntil();

		imgPlay.setOnClickListener(this);
		btnExit.setOnClickListener(this);
		mProgress.setOnSeekBarChangeListener(this);

		imgPlay.setBackgroundResource(R.drawable.recording);
		txtTitle.setText(_title + "(" + _date + ")");
		dialog.show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imgPlayNoteAudio:
			playingAudio();
			break;
		case R.id.btnExitPlaying:
			if (mThread != null) {
				if (mThread.isAlive()) {
					mThread.interrupted();
				}
			}
			this.stopPlaying();
			dialog.dismiss();
			break;
		default:
			break;
		}
	}

	/*
	 * Function save record after recording
	 */
	public void playingAudio() {
		if (mBlnPlay == true) {
			// Start record
			Log.v(TAG, "Playing");
			this.startPlaying(mPath);
			imgPlay.setBackgroundResource(R.drawable.recording_act);
			mBlnPlay = false;
		} else {
			Log.v(TAG, "Stopping");
			this.stopPlaying();
			imgPlay.setBackgroundResource(R.drawable.recording);
			mBlnPlay = true;
		}
	}

	/*
	 * Start playing an audio file
	 */
	public void startPlaying(String fileName) {
		mPlayer = new MediaPlayer();
		try {
			mPlayer.setDataSource(fileName);
			mPlayer.prepare();
			mPlayer.start();
			mProgress.setProgress(0);
			mProgress.setMax(mPlayer.getDuration());
			mThread = new Thread(this);
			mThread.start();
		} catch (IOException e) {
			Log.e(TAG, "prepare() failed");
		}
	}

	/*
	 * Stop playing an audio file
	 */
	public void stopPlaying() {
		if (mPlayer != null) {
			mPlayer.release();
			mPlayer = null;
		}
		if (mThread != null) {
			if (mThread.isAlive()) {
				mThread.interrupted();
			}
		}
	}

	// Thread run progress bar
	public void run() {
		int currentPosition = 0;
		int total = mPlayer.getDuration();
		while (mPlayer != null && currentPosition < total) {
			try {
				Thread.sleep(1);
				currentPosition = mPlayer.getCurrentPosition();
			} catch (InterruptedException e) {
				return;
			} catch (Exception e) {
				return;
			}
			mProgress.setProgress(currentPosition);
		}
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}
}
